require.config({
	paths: {
		"jquery":"jquery-1.11.3",
		"cookie":"jquery.cookie",
		"Animation":"requestAnimationFrame"
	}
});
//main.js
require(["jquery","cookie","Animation"],function($,cookie,Animation){
	$.getJSON("../json/LAMIU_one.json",function(json){
		//头部
		$("#headerCon a").hover(
			function(){
				$(this).css("color","rgb(204,204,204)");
			},
			function(){
				$(this).css("color","#000");
			}
		);
		//如果能获取到登录账号密码cookie值 就改掉主页的内容
		var users = $.cookie("loginedUsers") ? $.cookie("loginedUsers") : "" ;
		if(users){
			$(".loginIn a").html("欢迎您，尊敬的用户"+users);
			$(".registerOne a").html("");				
		}
		//search栏
		$("#hot").children().eq(1).html(json.search1);
		$("#hot").children().eq(2).html(json.search2);
		$("#hot").children().eq(3).html(json.search3);
		$("#forSale").attr("src",json.forSale);
		//console.log($("#headerCon a"));
		
		//menu栏
		var $VMenuList = $("#verticalMenu").children();
		$VMenuList.each(function(){
			$(this).css("background","url("+ json.MenubottomImg +") no-repeat center bottom");
		});
		//点击横向列表里的首页 跳转到首页
		$(".horMenuOne").click(function(){
			location.href = "LAMIU_one.html";
		});
		var $HMenuList = $("#horizontalMenu").children();
		$HMenuList.not("#horMenuOne").not("#allGoods").each(function(){
			$(this).hover(
				function(){
					$(this).css("color","#f19ec2");
				},
				function(){
					$(this).css("color","#000");
				}
			);
		});
		$("#verticalMenu li>a").hover(
			function(){
				$(this).css("font-weight","700");
			},
			function(){
				$(this).css("font-weight","300");
			}
		);
		//购物车
		$("#shoppingCart").hover(
			function(){
				$(this).first().css({"background-color":"white","border":"1px solid #e9e9e9e","height":"42px"});
				$("#goodsList").show();
			},
			function(){
				$("body").click(function(){
					$("#shoppingCart").first().css({"background":"none","border":"none","height":"40px"});
					$("#goodsList").hide();
				});
			}
		);
		//侧边栏
			$("#slidebar").find(".pinkA").hover(
				function(){
					$(this).css("background-color","#f53998");
				},
				function(){
					$(this).css("background-color","#000");
				}	
			);
			
			$("#cart_qqSevice").click(
				function(){
					$("#qqSevice_show").show();
				}
			);
			$("#back_btn").click(
				function(){
					$("#qqSevice_show").hide();
				}
			);
			/*回到顶部*/
			$("#slide_backToTop").click(
				function(){
					$("body").scrollTop(0);
				}
			);

		
		//往商品详情页添加内容
		//自定义属性 id
		var $PicId = $.cookie("PicId") ? $.cookie("PicId") : "";
		//图的数量
		var $len = json.goodshref[$PicId].bigPic.length;
		//小型图 字符串转成数组
		var $smallPic = $.cookie("smallPic") ? $.cookie("smallPic") : "";
		var $smallArr = $smallPic.split(",");
		//中型图 字符串 转成数组
		var $middlePic = $.cookie("middlePic") ? $.cookie("middlePic") : "";
		var $MidArr = $middlePic.split(",");
		//大型图 字符串转成数组
		var $bigPic = $.cookie("bigPic") ? $.cookie("bigPic") : "";
		var $bigArr = $bigPic.split(",");
		
		for(var j = 0;j < $len;j ++){
			//小型图添加
			var $j = "" + (j+1);
			var $href = ".PicPic" + $j;
			var $smallPic = '<img src="'+$smallArr[j]+'">';
			$($href).append($smallPic);
			//中型图添加
			var $middlePic = '<img src="'+$MidArr[j]+'">';
			$(".PicPic0").append($middlePic);
			//大型图添加
			var $bigPic = '<img src="'+$bigArr[j]+'">';
			$(".bigPicShower").append($bigPic);
		}
		
		/*进入中型图显示大图*/
		$(".PSlidePic").hover(
			function(){
				$(".bigPicShower").css({visibility:"visible",left:"576px"});
			},
			function(){
				$(".bigPicShower").css({visibility:"hidden",left:"-10000px"});
			}
		);
		//放大镜部分
		$(".PSlidePic").mouseenter(function(e){
			var $disX = e.pageX - $(this).offset().left;
			var $disY = e.pageY -$(this).offset().top;
//			$(".moveBox").css({
//				visibility: "visible"
//			});
			$(".PSlidePic").mousemove(function(e){
				var l = e.pageX - $(this).offset().left - parseInt($(".moveBox").css("width"))/2;
				var t = e.pageY -$(this).offset().top- parseInt($(".moveBox").css("height"))/2;
				//这里的left top 是相对于放中型图的盒子的 所以写边界时候 写中型图盒子的宽度减去滑块的宽度
				var $right = parseInt($(this).css("width")) - parseInt($(".moveBox").css("width"));
				var $bottom = parseInt($(this).css("height")) - parseInt($(".moveBox").css("height"));
				
				if(l < 0){
					l = 0;
				};
				if(l > $right){
					l = $right;
				};
				if(t < 0){
					t = 0;
				};
				if(t > $bottom){
					t = $bottom;
				};
				$(".moveBox").css({
					visibility: "visible",
					left: l,
					top: t
				});	
				//大图动
				var $rateX = parseInt($(".bigPicShower img").first().css("width"))/parseInt($(this).css("width"));
				var $rateY = parseInt($(".bigPicShower img").first().css("height"))/parseInt($(this).css("height"));
				
				$(".bigPicShower img").css({
					left: -l * $rateX,
					top: -t * $rateY
				});
			});
			$(".PSlidePic").mouseleave(function(){
				$(".moveBox").css({
					visibility: "hidden"
				});
			});
		});
		/*点击换图部分*/

		
		for(var i = 0;i < $len;i ++){
			var $i = "" + i;
			var $href = ".PicPic" + $i;
			//给列表里的小图加一个自定义属性 因为在鼠标移入事件里写 i 循环已经结束了 不能使用i变量
			$(".smallPicWrap ul").children().eq(i).attr("index",i);
			//边框变化
			var $cursorOn = $(".smallPicWrap ul").children().eq(i);
			//让第一张图默认显示
			$(".PicPic0 img").eq(0).css("zIndex","50");
			$(".bigPicShower img").eq(0).css("zIndex","50");
			$cursorOn.mouseenter(function(){
				//首先清除所有li的这个类名
				$(".smallPicWrap ul").children().removeClass("active");
				//给当前鼠标经过的这个li 加上这个类名
				$(this).addClass("active");
				$(".PicPic0 img").css("zIndex","0");
				$(".PicPic0 img").eq($(this).attr("index")).css("zIndex","50");
				$(".bigPicShower img").eq($(this).attr("index")).css("zIndex","50");
			});
		};
		//传入title区域的内容
		var $title = $.cookie("title");
		var $titleArr = $title.split(",");
		for(var i = 0;i < 5;i ++){
			var $i = "" + i;
			var $titlehref = ".title" + $i;
			$($titlehref).append($titleArr[i]);
		}
		var $ColorImg ='<img src="'+$titleArr[5]+'">';
		$(".title5").append($ColorImg);
		//位置
		$(".position0").append($titleArr[0]);
		
		//传入size区的内容
		var $size = $.cookie("size");	
		var $sizeArr = $size.split(",");
		$(".title6").append($sizeArr[0]);
		for(var i = 1;i < $sizeArr.length;i ++){
			var $i = "" + i;
			var $sizehref1 = "size" + $i;
			var $sizehref2 = ".size" + $i;
			var $sizeLi = '<li class="sizeNum">'+
							'<a href="javascript:void(0);">'+
								'<span class="'+$sizehref1+'"></span>'+
							'</a>'+
						  '</li>';
			$(".BrasSizeUl").append($sizeLi);
			$($sizehref2).append($sizeArr[i]);
		}
		//给购买套餐区加
		
		
		
		var $favourable = $.cookie("favourable");
		var $favourableArr = $favourable.split(",");
		if($favourable){
			/*动态生成优惠套餐区*/
			var $favourableArea = 	'<!--优惠套餐-->'+
									'<div class="forSaleCon">'+
										'<div class="forSaleCon_img">'+
											'<a href="#" class="favourable0"></a>'+
										'</div>'+
										'<ul class="forSaleCon_ul">'+
											'<li>'+
												'<span>颜色:</span>'+
												'<span class="damnIt favourable1 active"></span>'+
											'</li>'+
											'<li>'+
												'<span>底裤尺码:</span>'+
												'<span class="damnIt favourable2 active"></span>'+
											'</li>'+
										'</ul>'+
									'</div>'+
									'<!--套餐购买-->'+
									'<div class="forSaleBuy">'+
										'<li class="forSaleBuyCon">'+
											'<div class="fl">'+
												'<div class="spanBox">'+
													'<span class="forSaleCost">'+
														'折扣价:'+
														'<i class="favourable3"></i>'+
													'</span>'+
												'</div>'+
												'<div class="spanBox">'+
													'<span class="originCost">'+
														'售价：'+
														'<del class="favourable4"></del>'+
													'</span>'+
												'</div>'+
											'</div>'+
											'<button class="forSaleBuying">'+
												'<i class="iconfont">&#xe698;</i>'+
												'<span>购买套餐</span>'+
											'</button>'+
										'</li>'+
									'</div>'+
								'</div>';
			$(".productForSale").append($favourableArea);				
		}
		for(var i = 1;i < $favourableArr.length;i ++){
			var $i = "" + i;
			var $favourablehref = ".favourable" + $i;
			$($favourablehref).append($favourableArr[i]);
		}
		var $favourableImg = '<img src="'+$favourableArr[0]+'">';
		$(".favourable0").append($favourableImg);
		//选中的号码变色		
		 for(var i = 0;i < $(".BrasSizeUl li").length;i ++){
		 	$(".sizeNum").eq(i).click(function(){
		 		$(".sizeNum").removeClass("active");
		 		$(this).addClass("active");
		 	});
		 }
		//选中颜色尺码变色
		$(".forSaleCon_ul .damnIt").removeClass("active");
		$(".forSaleCon_ul .damnIt").eq(0).click(function(){
			$(this).addClass("active");
		});
		$(".forSaleCon_ul .damnIt").eq(1).click(function(){
			$(this).addClass("active");
		});
		//点击加入购物车
		loadCart();
		$(".addToCart").click(function(){
			$(".successAdd").css("display","block");
			//点击加入购物车加到cookie中
			var $proId = $.cookie("PicId");
			var $proName = $(".title0").html();
			//把￥149 改成149
			var Price = $(".title1").html();
			var pattern = /(\d+)/;
			var $proPrice = parseInt(Price.match(pattern)[1]);
			var $proImg = $(".title5").html();
			console.log($proImg);
			var $proSize = $(".BrasSizeUl .active").find("span").html();
			var $proColor = $(".favourable1").html();
			//主要部分
			var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
			var cartObj = convertCartStrToObj(cartStr);
			if($proId in cartObj){
				cartObj[$proId].num += 1;
			}else{
				cartObj[$proId] = {
					name : $proName,
					price : $proPrice,
					num : 1,
					src : $proImg,
					size : $proSize,
					color : $proColor
				};
			}
			cartStr = convertObjToCartStr(cartObj);
			$.cookie("cart",cartStr,{expires : 7,path : "/"});
			console.log($.cookie("cart"));	
		});
		$(".close").click(function(){
			$(".successAdd").css("display","none");
		});
		$(".continuteBuy").click(function(){
			$(".successAdd").css("display","none");
		});
		$(".goToCart").click(function(){
			location.href = "shoppingCart.html";
		});
		
		//字符串转对象
		function convertCartStrToObj(cartStr){
				//"sp1,香蕉,30,1,src1:sp2,苹果,40,2,src2:sp3,梨,50,3,str3"
				//如果是空字符串，即没有购物车信息，那么购物车为空，直接返回一个空对象
				if(!cartStr){
					return {};
				}
				var goods = cartStr.split(":");
				var obj = {};
				for(var i = 0; i < goods.length; i ++){
					var data = goods[i].split(",");
					//以商品的id为健，商品的其他信息为值，这个值也设计为一个对象
					obj[data[0]] = {
						name : data[1],
						price : parseFloat(data[2]),
						num : parseInt(data[3]),
						src : data[4],
						size : data[5],
						color : data[6]
					}
				}
				return obj;
			}
			//对象转字符串.
			function convertObjToCartStr(obj){
					
					var cartStr = "";
					//遍历对象
					for(var id in obj){
						if(cartStr){
							cartStr += ":";
						}
						//"sp1,香蕉,30,1,src1:sp2,苹果,40,2,src2:sp3,梨,50,3,str3"
						cartStr += id + "," + obj[id].name + "," + obj[id].price + "," + obj[id].num + "," + obj[id].src + "," + obj[id].size + "," + obj[id].color;
					}
					return cartStr;
			}
			
			//加载购物车中的信息（使商品页与购物车页中的购物车数量同步）
			function loadCart(){
				var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
					var cartObj = convertCartStrToObj(cartStr);
					//获取到购物车中所有商品的数量
					var total = 0;
					for(var id in cartObj){
						total += cartObj[id].num;
					}
					$(".proNum").html(total+1);
			}
	});
});
